﻿<?php @session_start();?>
<?php

//include "dbconnect.php";

      
function play($play_1, $play_2, $won = array(1, 2, 0)){
        
            return ($play_1 != $play_2) ? ($won[$play_1] == $play_2) ? 1 : 0 : 2 ;
            
   }
   if ($_POST['new'])
   {
          session_destroy();
   }
   if (isset($_POST['submit']))
   {
        $player = $_POST['game'];
        $computer = rand(0, 2);
        $_SESSION['Score'] == $_SESSION['Score'];
  
        echo '<strong>';
        echo 'Arvutil oli ';
        
        if ($computer == 0)
        {
            echo 'kivi';
        }
        elseif ($computer == 1)
        {
            echo 'käärid';
        }
        elseif ($computer == 2)
        {
            echo 'paber';
        }
  
  
        echo ' ja sinul oli ';
        if ($player == 0)
        {
            echo 'kivi';
        }
        elseif ($player == 1)
        {
            echo 'käärid';
        }
        elseif ($player == 2)
        {
            echo 'paber';
        }
      
        echo '</strong>';
        echo '<br /><br />';
      
        if(play($player, $computer) == 0)
        {
            echo '<span style="color: red;"><strong>KAOTASID!</strong></span>';
          if(isset($_SESSION['Score']))
          {
            $_SESSION['Score'] = $_SESSION['Score'] -1;
          }
          else
            $_SESSION['Score']= -1;
        }
        elseif(play($player, $computer) == 1)
        {
              echo '<span style="color: green;"><strong>VÕITSID!</strong></span>';
        if(isset($_SESSION['Score']))
          {
            $_SESSION['Score'] = $_SESSION['Score'] +1;
          }
        else
          $_SESSION['Score']= 1;
        }
        elseif(play($player, $computer) == 2)
        {
             echo '<span style="color: black;"><strong>VIIK!</strong></span>';
            if(isset($_SESSION['Score']))
            {
              $_SESSION['Score']= $_SESSION['Score'];
            }
            else
              $_SESSION['Score']= 0;
        }
        
        if ($_POST['new'])
        {
          session_destroy();
        }
        
        echo '<br /><br />';
        echo 'Sinu punktid: '.$_SESSION['Score'].'</br></br> ';
        echo '<form method="post" action= '. $_SERVER['PHP_SELF'].'>
        Tee oma valik:&nbsp;<br />
        <input type="radio" name="game" value="0" /> Kivi<br />
        <input type="radio" name="game" value="1" /> Käärid<br />
        <input type="radio" name="game" value="2" /> Paber<br />
        <br /><br />
        <input type="submit" name="submit" value="Mängi!">
        <input type="submit" name="new" value="Nulli tulemus">
        </form>';
        
        
  
    }
    else
    {
        ?>
        <h2> Kivi käärid paber</h2></br></br></br>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        Tee oma valik:&nbsp;<br />
        <input type="radio" name="game" value="0" /> Kivi<br />
        <input type="radio" name="game" value="1" /> Käärid<br />
        <input type="radio" name="game" value="2" /> Paber<br />
        <br /><br />
        <input type="submit" name="submit" value="Mängi!">
        </form>
        <?php
    }
?>